/*
 * @Author: silence
 * @Date: 2023-07-04 11:39:56
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-08-01 16:56:11
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import 'package:syncfusion_flutter_charts/charts.dart';

import '../../../utils/color/ColorUtils.dart';
import '../../Mine/Language/SilLocalizations/SilLocalizations.dart';
import '../../PowerStation/Controller/PowerStationController.dart';

class HomePowerProgressView extends StatefulWidget {
  const HomePowerProgressView({super.key});

  @override
  State<HomePowerProgressView> createState() => _HomePowerProgressViewState();
}

class _HomePowerProgressViewState extends State<HomePowerProgressView> {
  late TooltipBehavior _tooltip;
  PowerStationController _powerStationController =
      Get.put(PowerStationController());

  @override
  void initState() {
    _tooltip = TooltipBehavior(enable: true);
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return mainView();
  }

  mainView() {
    return Container(
      margin: EdgeInsets.only(top: 30.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [Obx(() => remainingView()), Obx(() => realTimePowerView())],
      ),
    );
  }

  /// 剩余电量
  remainingView() {
    String remainingValue =
        _powerStationController.powerStationEntity.value.surplus_power ?? '0';
    int remainingValueInt = double.parse(remainingValue).toInt();
    String circleHexColor = _powerStationController
        .powerStationEntity.value.surplusPowerCircleHexColor;
    return Container(
      height: 125.w,
      width: 125.w,
      child: Stack(
        alignment: Alignment.center,
        children: [
          circleView(
              value: double.parse(remainingValue), circleColor: circleHexColor),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 22.w),
            alignment: Alignment.center,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SilText(
                  LangOf(context)?.index?.remaining,
                  fontSize: 12.sp,
                  color: ColorUtils.color_font_main,
                  margin: EdgeInsets.only(bottom: 5.w),
                  maxLines: 0,
                  textAlign: TextAlign.center,
                ),
                SilText(
                  '$remainingValueInt%',
                  fontSize: 28.sp,
                  color: ColorUtils.color_font_main,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 实时功率
  realTimePowerView() {
    double realPowerPer =
        _powerStationController.powerStationEntity.value.realPowerPer;
    String realPower =
        _powerStationController.powerStationEntity.value.real_power ?? "0";
    int realPowerInt = double.parse(realPower).toInt();
    String circleHexColor = _powerStationController
        .powerStationEntity.value.realPowerPerCircleHexColor;

    if(realPowerPer > 0 && realPowerPer < 1){
      realPowerPer = 1;
    }

    return Container(
      height: 125.w,
      width: 125.w,
      child: Stack(
        alignment: Alignment.center,
        children: [
          circleView(value: realPowerPer, circleColor: circleHexColor),
          Container(
              padding: EdgeInsets.symmetric(horizontal: 22.w),
              alignment: Alignment.center,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SilText(
                    LangOf(context)?.index?.real_time,
                    fontSize: 12.sp,
                    color: ColorUtils.color_font_main,
                    margin: EdgeInsets.only(bottom: 3.w, top: 12.w),
                    maxLines: 0,
                    textAlign: TextAlign.center,
                  ),
                  SilText(
                    "$realPowerInt",
                    fontSize: 28.sp,
                    color: ColorUtils.color_font_main,
                    margin: EdgeInsets.only(bottom: 3.w),
                    maxLines: 0,
                    textAlign: TextAlign.center,
                  ),
                  SilText(
                    'W',
                    fontSize: 14.sp,
                    color: ColorUtils.color_font_main,
                  ),
                ],
              )),
        ],
      ),
    );
  }

  circleView({double value = 0, String circleColor = '#c81410'}) {
    double radius = 125.w / 2;
    double innerRadius = radius - 4.5.w;
    return SfCircularChart(
      // backgroundColor: Colors.lightGreen,
      tooltipBehavior: _tooltip,
      margin: EdgeInsets.zero,
      series: <CircularSeries>[
        RadialBarSeries<_ChartData, String>(
          legendIconType: LegendIconType.circle,
          dataSource: [_ChartData('', value)],
          xValueMapper: (_ChartData data, _) => data.name,
          yValueMapper: (_ChartData data, _) => data.y,
          pointColorMapper: (datum, index) => ColorUtils.hexColor(circleColor),
          trackColor: ColorUtils.hexColor('#666666'),
          innerRadius: "$innerRadius",
          radius: '$radius',
          enableTooltip: false, // 不显示提示
          cornerStyle: CornerStyle.bothFlat,
          maximumValue: 100,
        ),
      ],
    );
  }
}

class _ChartData {
  _ChartData(this.name, this.y);

  final String name;
  final double y;
}
